package com.qf.dao.impl;

import com.qf.dao.ProjectDao;
import com.qf.dto.ProjectDto;
import com.qf.pojo.Developer;
import com.qf.pojo.Project;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName ProjectDapImpl
 * @Description TODO
 * @Author Windy
 * @Date 2022/9/2 15:55
 * @Version 1.0
 **/
@Repository
public class ProjectDaoImpl implements ProjectDao {
    @Autowired
    JdbcTemplate jdbcTemplate;

    @Override
    public ProjectDto findProjectById(int pid) {
        List<Project> query = jdbcTemplate.query("select * from project where pid=?;",
                new BeanPropertyRowMapper<Project>(Project.class), pid);
        if (query.size() <= 0) {
            return null;
        }
        ProjectDto dto = new ProjectDto();
        List<Developer> developerList = jdbcTemplate.query("select * from developer where did=?",
                new BeanPropertyRowMapper<Developer>(Developer.class), query.get(0).getDid());

        dto.setProject(query.get(0));
        if (developerList.size() > 0) {
            dto.setDeveloper(developerList.get(0));
        }
        return dto;
    }

    @Override
    public ProjectDto findProjectByName(String name) {
        List<Project> query = jdbcTemplate.query("select * from project where pname=?;",
                new BeanPropertyRowMapper<Project>(Project.class), name);
        if (query.size() <= 0) {
            return null;
        }
        ProjectDto dto = new ProjectDto();
        List<Developer> developerList = jdbcTemplate.query("select * from developer where did=?",
                new BeanPropertyRowMapper<Developer>(Developer.class), query.get(0).getDid());

        dto.setProject(query.get(0));
        if (developerList.size() > 0) {
            dto.setDeveloper(developerList.get(0));
        }
        return dto;
    }

    @Override
    public int deletedProject(Project project) {
        int update = jdbcTemplate.update("update project set status=0 where pid=? ", project.getPid());
        int i=0;
        if (update >= 0) {
            i = jdbcTemplate.update("update stage set pid=0 where  pid=?", project.getPid());
        }else {
            return -1;
        }
        return i;
    }

    @Override
    public int updateProjectById(Project project) {
        Object[] obj={project.getPname(),project.getStarttime(),project.getEndtime(),project.getDid(),
            project.getCid(),project.getUpdatetime(),project.getStatus(),project.getPid()};
        int update = jdbcTemplate.update("update project set pname=?,starttime=?,endtime=?,did=?,cid=?,updatetime=?," +
                "status=?  where pid=?", obj);
        return update;
    }

    @Override
    public int insertProject(Project project) {
        Object[] obj={project.getPid(),project.getPname(),project.getStarttime(),project.getEndtime(),project.getDid(),
                project.getCid(),project.getUpdatetime(),project.getStatus()};
        int update = jdbcTemplate.update("insert into project values(?,?,?,?,?,?,?,?)", obj);
        return update;
    }

    @Override
    public List<ProjectDto> findAll() {
        List<Project> query = jdbcTemplate.query("select * from project;",
                new BeanPropertyRowMapper<Project>(Project.class));
        List<ProjectDto> lists=new ArrayList<>();
        for (Project project : query) {
            ProjectDto dto = new ProjectDto();

            List<Developer> developerList = jdbcTemplate.query("select * from developer where did=?",
                    new BeanPropertyRowMapper<Developer>(Developer.class), project.getDid());

//            System.out.println(project);
            dto.setProject(project);
            if (developerList.size() > 0) {
                dto.setDeveloper(developerList.get(0));
            }

            lists.add(dto);
        }
        return lists;
    }
}
